<?php
/**
 * 报表管理
 */
class ReportAction extends BaseAction{
	
	/**
	 * 调用模版展示订单报表
	 */
	public function orderReport(){
		$this->display();
	}
	
	/**
	 * 根据开始时间和结束时间按照特定的报表类型来组织标题数据
	 * @param unknown_type $iType 报表类型
	 * @param unknown_type $sStartTime 开始时间
	 * @param unknown_type $sEndTime 结束时间
	 */
	private function organizeTime($iType, $sStartTime, $sEndTime){
		$arrReturn = array();
		$sStartTimeStamp = $this->getTimeStamp($sStartTime, '-');
		$sEndTimeStamp = $this->getTimeStamp($sEndTime, '-');
		
		//log::write("$sStartTimeStamp-$sEndTimeStamp");
		switch((int)$iType){
			case 0:
				$arrReturn = $this->organizeTimeByDay($sStartTimeStamp, $sEndTimeStamp);
				break;
			case 1:
				$arrReturn = $this->organizeTimeByMonth($sStartTimeStamp, $sEndTimeStamp);
				break;
			case 2:
				break;
			default:
				break;
		}
		//log::write(serialize($arrReturn));
		return $arrReturn;

	}
	
	/**
	 * 获取某个格式日期的时间戳
	 * 注意：该方法只适合于日期，不能加时间
	 * @param unknown_type $sTime 时间
	 * @param unknown_type $sInterval 分隔符
	 */
	private function getTimeStamp($sTime, $sInterval='/'){
		list($s_year, $s_month, $s_day) = explode($sInterval, $sTime);
        $mk_time = mktime(0, 0, 0, $s_month, $s_day, $s_year);
        return $mk_time; 
	}
	
	/**
	 * 根据开始时间和结束时间按照天来组织数据
	 * @param unknown_type $sStartTimeStamp 开始时间戳
	 * @param unknown_type $sEndTimeStamp 结束时间戳
	 */
	private function organizeTimeByDay($sStartTimeStamp, $sEndTimeStamp){
		$arrResult = array();
		$iOffset = $sEndTimeStamp - $sStartTimeStamp;
     	$iOneDay = 24*3600;
     	
		if($iOffset < $iOneDay){
			$arrResult[date('Ymd', $sStartTimeStamp)] = array();
		}
		for($i = $sStartTimeStamp; $i <= $sEndTimeStamp; $i += $iOneDay) {
			$arrResult[date('Ymd', $i)] = array();
		}
		
		return $arrResult;
	}
	
	/**
	 * 根据开始时间和结束时间按照月来组织数据
	 * @param unknown_type $sStartTimeStamp 开始时间戳
	 * @param unknown_type $sEndTimeStamp 结束时间戳
	 */
	private function organizeTimeByMonth($sStartTimeStamp, $sEndTimeStamp){
		$arrResult = array();
		
		$sStartDate = date('Y-m', $sStartTimeStamp).'-01';
		$sEndDate = date('Y-m', $sEndTimeStamp).'-01';
		
		//转为时间戳
		$sStartDateStamp = strtotime($sStartDate);
		$sEndDateStamp = strtotime($sEndDate);
		
		$t = $sStartDateStamp;
		while($t <= $sEndDateStamp)
		{
			//累加每个月的的总秒数 计算公式：上一月1号的时间戳秒数减去当前月的时间戳秒数
			$arrResult[date('Ym', $t)] = array();
			$t += strtotime('+1 month', $t) - $t;
		}
		return $arrResult;
	}
	
	public function getOrderXml(){
		$iType = $_POST['ordertype'];
		$sStartTime = $_POST['starttime'];
		$sEndTime = $_POST['endtime'];
		//log::write("$iType-$sStartTime-$sEndTime");
		$arrReturn = $this->organizeTime($iType, $sStartTime, $sEndTime);
		
		switch((int)$iType){
			case 0:
				$sSql = "SELECT status, DATE_FORMAT(ctime, '%Y%m%d') AS rtime, COUNT(*) AS c FROM ticket.v1_order_status WHERE ctime BETWEEN '$sStartTime' AND '$sEndTime' GROUP BY status, rtime";
				break;
			case 1:
				$sSql = "SELECT status, DATE_FORMAT(ctime, '%Y%m') AS rtime, COUNT(*) AS c FROM ticket.v1_order_status WHERE ctime BETWEEN '$sStartTime' AND '$sEndTime' GROUP BY status, rtime";
				break;
			default:
				$sSql = "SELECT status, DATE_FORMAT(ctime, '%Y%m%d') AS rtime, COUNT(*) AS c FROM ticket.v1_order_status WHERE ctime BETWEEN '$sStartTime' AND '$sEndTime' GROUP BY status, rtime";
				break;
		}
		
		$arrOrders = $this->DBSelect($sSql);
		$sReturnXml = "<chart palette='3' caption='订单状态报表'  numberprefix='' xaxisName='时间' useRoundEdges='1' showValues='0' legendBorderAlpha='0'>";
		
		foreach($arrOrders as $oOrder){
			
		}
		
		echo "<chart palette='3' caption='订单状态报表'  numberprefix='' xaxisName='日' useRoundEdges='1' showValues='0' legendBorderAlpha='0'>  
				<categories>
				      <category label='1' />
				      <category label='2' />
				      <category label='3' />
				      <category label='4' />
				      <category label='5' />
				      <category label='6' />
				      <category label='7' />
				      <category label='8' />
				      <category label='9' />
				      <category label='10' />
				      <category label='11' />
				      <category label='12' />
				      <category label='13' />
				      <category label='14' />
				      <category label='15' />
				      <category label='16' />
				      <category label='17' />
				      <category label='18' />
				      <category label='19' />
				      <category label='20' />
				      <category label='21' />
				      <category label='22' />
				      <category label='23' />
				      <category label='24' />
				      <category label='25' />
				      <category label='26' />
				      <category label='27' />
				      <category label='28' />
				      <category label='29' />
				      <category label='30' />
				      <category label='31' />      
				</categories>
				
				<dataset seriesname='未支付'>
				      <set value='57000' />
				      <set value='58995' />
				      <set value='61119' />
				      <set value='63502' />
				      <set value='57000' />
				      <set value='58995' />
				      <set value='61119' />
				      <set value='63502' />
				      <set value='65662' />
				      <set value='68157' />
				      <set value='69997' />
				      <set value='72797' />
				      <set value='75272' />
				      <set value='77455' />
				      <set value='79546' />
				      <set value='80739' />
				      <set value='82273' />
				      <set value='84001' />
				      <set value='85849' />
				      <set value='85868' />
				      <set value='87843' />
				      <set value='90039' />
				      <set value='61452' />
				      <set value='64463' />
				      <set value='65945' />
				      <set value='68056' />
				      <set value='70914' />
				      <set value='73467' />
				      <set value='76332' />
				      <set value='78927' />
				      <set value='81927' />      
				</dataset>
				
				<dataset seriesname='已支付'>
				      <set value='15500' />
				      <set value='16361' />
				      <set value='17212' />
				      <set value='18443' />
				      <set value='15500' />
				      <set value='16361' />
				      <set value='17212' />
				      <set value='18443' />
				      <set value='19270' />
				      <set value='20782' />
				      <set value='21128' />
				      <set value='22606' />
				      <set value='23915' />
				      <set value='24982' />
				      <set value='26436' />
				      <set value='26810' />
				      <set value='27421' />
				      <set value='28162' />
				      <set value='30000' />
				      <set value='28965' />
				      <set value='29767' />
				      <set value='29559' />
				      <set value='26150' />
				      <set value='28349' />
				      <set value='28676' />
				      <set value='29221' />
				      <set value='30681' />
				      <set value='31665' />
				      <set value='33109' />
				      <set value='34062' />
				      <set value='35849' />      
				</dataset>
				
				<dataset seriesname='待配送'>
				      <set value='38000' />
				      <set value='39330' />
				      <set value='40746' />
				      <set value='42335' />
				      <set value='38000' />
				      <set value='39330' />
				      <set value='40746' />
				      <set value='42335' />
				      <set value='43774' />
				      <set value='45438' />
				      <set value='46665' />
				      <set value='48531' />
				      <set value='50181' />
				      <set value='51637' />
				      <set value='53031' />
				      <set value='53826' />
				      <set value='54849' />
				      <set value='56001' />
				      <set value='57233' />
				      <set value='57245' />
				      <set value='58562' />
				      <set value='60026' />
				      <set value='40968' />
				      <set value='42975' />
				      <set value='43964' />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				</dataset>
				
				<dataset seriesname='待退货'>
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set />
				      <set value='10494' />
				      <set value='29221' />
				      <set value='30681' />
				      <set value='31665' />
				      <set value='33109' />
				      <set value='34062' />
				</dataset>
			</chart>";
		
	}
}
?>